# Maintainer: zxp19821005 <zxp19821005 at 163 dot com>
# Contributor: John Regan <john@jrjrtech.com>
pkgbase=libvgm-git
pkgname=('libvgm-player-git' 'libvgm-emu-git' 'libvgm-utils-git' 'libvgm-audio-git' 'libvgm-common-git' 'vgm2wav-git' 'vgmplayer-git')
pkgver=r578.798cb20
pkgrel=1
pkgdesc="Library for decoding and playing VGM files"
arch=('any')
url="https://github.com/ValleyBell/libvgm"
license=('GPL-3.0-only')
makedepends=(
    'zlib'
    'alsa-lib'
    'libpulse'
    'libao'
    'git'
    'cmake'
)
source=("${pkgbase//-/.}::git+${url}.git")
sha256sums=('SKIP')
pkgver() {
    cd "${srcdir}/${pkgbase//-/.}"
    set -o pipefail
    git describe --long --tags --abbrev=7 | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/v//g' ||
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short=7 HEAD)"
}
prepare() {
    cd "${srcdir}/${pkgbase//-/.}"
    # leaving a note for patching
    #patch --forward --strip=1 --input="${srcdir}/libvgm-fix-strncasecmp.patch"
}
build() {
	cd "${srcdir}/${pkgbase//-/.}"
    rm -rf build
    mkdir build
    cd build
    cmake \
        -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-Bsymbolic -Wl,-Bsymbolic-functions" \
        -DCMAKE_MODULE_LINKER_FLAGS="-Wl,-Bsymbolic -Wl,-Bsymbolic-functions" \
        -DLIBRARY_TYPE=SHARED \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_SKIP_BUILD_RPATH=TRUE \
        -DCMAKE_INSTALL_PREFIX=/usr \
        ..
	make
}
package_libvgm-common-git() {
    pkgdesc="libvgm common headers"
	cd "${srcdir}/${pkgbase//-/.}/build"
	make DESTDIR="${pkgdir}/" install

    rm -rf "${pkgdir}/usr/bin"
    rm -rf "${pkgdir}/usr/lib/"{libvgm-audio*,libvgm-emu*,libvgm-player*,libvgm-utils*}
    rm -rf "${pkgdir}/usr/lib/pkgconfig/"{vgm-audio.pc,vgm-emu.pc,vgm-player.pc,vgm-utils.pc}
    rm -rf "${pkgdir}/usr/include/vgm/"{audio,emu,player,utils}
}

package_libvgm-audio-git() {
    pkgdesc="libvgm audio playing library"
    depends=('libvgm-common-git' 'alsa-lib' 'libpulse' 'libao')
	cd "${srcdir}/${pkgbase//-/.}/build"
	make DESTDIR="${pkgdir}/" install
    rm -rf "${pkgdir}/usr/bin"
    rm -rf "${pkgdir}/usr/lib/"{libvgm-emu*,libvgm-player*,libvgm-utils*}
    rm -rf "${pkgdir}/usr/lib/pkgconfig/"{vgm-emu.pc,vgm-player.pc,vgm-utils.pc}
    rm -rf "${pkgdir}/usr/include/vgm/"{emu,player,utils}
    rm -rf "${pkgdir}/usr/lib/cmake"
    rm -rf "${pkgdir}/usr/include/vgm/"{common_def.h,_stdbool.h,stdtype.h}
}
package_libvgm-emu-git() {
    pkgdesc="libvgm emulation library"
    depends=('libvgm-common-git')
	cd "${srcdir}/${pkgbase//-/.}/build"
	make DESTDIR="${pkgdir}/" install
    rm -rf "${pkgdir}/usr/bin"
    rm -rf "${pkgdir}/usr/lib/"{libvgm-audio*,libvgm-player*,libvgm-utils*}
    rm -rf "${pkgdir}/usr/lib/pkgconfig/"{vgm-audio.pc,vgm-player.pc,vgm-utils.pc}
    rm -rf "${pkgdir}/usr/include/vgm/"{audio,player,utils}
    rm -rf "${pkgdir}/usr/lib/cmake"
    rm -rf "${pkgdir}/usr/include/vgm/"{common_def.h,_stdbool.h,stdtype.h}
}
package_libvgm-player-git() {
    pkgdesc="libvgm player library"
    depends=('libvgm-common-git' 'libvgm-emu-git' 'libvgm-utils-git')
	cd "${srcdir}/${pkgbase//-/.}/build"
	make DESTDIR="${pkgdir}/" install
    rm -rf "${pkgdir}/usr/bin"
    rm -rf "${pkgdir}/usr/lib/"{libvgm-audio*,libvgm-emu*,libvgm-utils*}
    rm -rf "${pkgdir}/usr/lib/pkgconfig/"{vgm-audio.pc,vgm-emu.pc,vgm-utils.pc}
    rm -rf "${pkgdir}/usr/include/vgm/"{audio,emu,utils}
    rm -rf "${pkgdir}/usr/lib/cmake"
    rm -rf "${pkgdir}/usr/include/vgm/"{common_def.h,_stdbool.h,stdtype.h}
}
package_libvgm-utils-git() {
    pkgdesc="libvgm utility library"
    depends=('libvgm-common-git' 'zlib')
	cd "${srcdir}/${pkgbase//-/.}/build"
	make DESTDIR="${pkgdir}/" install
    rm -rf "${pkgdir}/usr/bin"
    rm -rf "${pkgdir}/usr/lib/"{libvgm-audio*,libvgm-emu*,libvgm-player*}
    rm -rf "${pkgdir}/usr/lib/pkgconfig/"{vgm-audio.pc,vgm-emu.pc,vgm-player.pc}
    rm -rf "${pkgdir}/usr/include/vgm/"{audio,emu,player}
    rm -rf "${pkgdir}/usr/lib/cmake"
    rm -rf "${pkgdir}/usr/include/vgm/"{common_def.h,_stdbool.h,stdtype.h}
}
package_vgm2wav-git() {
    pkgdesc="VGM to WAVE utility"
    depends=('libvgm-utils-git' 'libvgm-player-git')
	cd "${srcdir}/${pkgbase//-/.}/build"
    install -Dm755 bin/vgm2wav -t "${pkgdir}/usr/bin"
}

package_vgmplayer-git() {
    pkgdesc="Interactive VGM player"
    depends=('libvgm-utils-git' 'libvgm-player-git' 'libvgm-audio-git')
	cd "${srcdir}/${pkgbase//-/.}/build"
    install -Dm755 bin/player -t "${pkgdir}/usr/bin"
}